/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-07-09
 * Time: 14:56
 */

  class ListNode {
    int val;
   ListNode next;
   ListNode(int x) {
         val = x;
        next = null;
   }
 }
class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        ListNode curB = headB;
        int lenA = 0;
        int lenB = 0;
        //求链表1的长度
        while (curA != null) {
            lenA++;
            curA = curA.next;//curA发生改变
        }
        //求链表2的长度
        while (curB != null) {
            lenB++;
            curB = curB.next;//curB发生改变
        }

        //将curA和curB重新指向链表的起始位置
        curA = headA;
        curB = headB;

        //差值
        int len = lenA - lenB;

        //将较长的链表头指针后移差值个长度
        if (len < 0) {
            len = -len;
            while (len != 0) {
                curB = curB.next;
                len--;
            }
        }else {
            while (len != 0) {
                curA = curA.next;
                len--;
            }
        }

        //相遇处即为交点
        while (curA != curB) {
            curA = curA.next;
            curB = curB.next;
        }
        return curA;
    }
}
public class Test {
}
